// axios是request.js中封装的自定义的函数
import request from '@/utils/request'
export const getAllChannelListAPI = () => {
  return request({
    url: '/v1_0/channels'
  })
}

// 用户登录
export const loginAPI = ({ mobile, code }) => {
  // 知识点: axios的data选项如果是一个js对象,axios内部会帮你转成json字符串
  return request({
    url: '/v1_0/authorizations',
    method: 'post',
    data: {
      mobile: mobile,
      code: code
    }
  })
}

/**
 * 专门获取 用户已选频道列表
 * @headers Authorization: Bearer + token
 * @returns Promise对象
 */
export const getUserChannelAPI = () => request({
  url: '/v1_0/user/channels'

})

// 获取文章列表
export const getArticleListAPI = ({ channel_id, timestamp }) => {
  return request({
    url: '/v1_0/articles',
    params: {
      channel_id,
      timestamp
    }
  })
}

// 反馈不喜欢的文章
export const articleDislikeAPI = ({ target }) => {
  return request({
    url: '/v1_0/article/dislikes',
    method: 'post',
    data: {
      target: target
    }

  })
}

// 举报文章
export const articleReportsAPI = ({ target, type, remark }) => {
  // 自行组织参数对象
  const dataObj = {
    target: target,
    type
  }
  if (type === 0) {
    dataObj.remark = remark
  }
  return request({
    url: '/v1_0/article/reports',
    method: 'post',
    data: dataObj
  })
}

/**
 * 更新用户已选频道列表
 * @param {*} param0 {channels:频道列表}
 * @returns Promise对象
 */
// 设置频道列表
export const uploadUserChannelAPI = ({ channels }) => {
  return request({
    url: '/v1_0/user/channels',
    method: 'put',
    data: {
      channels
    }
  })
}

// 搜索联想接口
export const getSuggestListAPI = ({ keywords }) => {
  return request({
    url: '/v1_0/suggestion',
    params: {
      q: keywords
    }
  })
}

// 搜索结果
export const getSearchResultListAPI = ({ page, per_page, q }) => {
  return request({
    url: '/v1_0/search',
    params: {
      page,
      per_page,
      q
    }
  })
}

// 获取文章详情
export const getArticleDetailAPI = ({ artId }) => {
  return request({
    url: `/v1_0/articles/${artId}`
  })
}

// 文章 - 关注作者
export const followedUserAPI = ({ target }) => {
  return request({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target: target
    }
  })
}
// 文章 - 取消关注作者
export const unFollowedUserAPI = ({ uid }) => {
  return request({
    // 这uid只是个变量名, 把值拼接在url发给后台(无需指定参数名)
    url: `/v1_0/user/followings/${uid}`,
    method: 'DELETE'
  })
}

// 文章 - 点赞
export const articleLikeAPI = ({ target }) => {
  return request({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target: target
    }
  })
}

// 文章 - 取消点赞
export const articleDisLikeAPI = ({ artId }) => {
  return request({
    url: `/v1_0/article/likings/${artId}`,
    method: 'DELETE'
  })
}

// 获取评论数据
// 知识点1: 对象解构失败(传过来的对象里没有offset和limit属性-因为可选的, offset和limit变量的值就为=后面的值, 如果外面传入就不用=后面默认值了)
// 知识点2: axios只针对params选项的值做了判断, 如果你传递的参数值为null, axios会忽略掉这个参数不在url?后面携带
export const getArticleCommentListAPI = ({ artId, offset = null, limit = 10 }) => {
  return request({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source: artId,
      offset,
      limit
    }
  })
}

// 评论 - 喜欢
export const commentLikingAPI = ({ comId }) => {
  return request({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: comId
    }
  })
}
// 评论-取消喜欢
export const commentDisLikingAPI = ({ comId }) => {
  return request({
    url: `/v1_0/comment/likings/${comId}`,
    method: 'DELETE'
  })
}

export const sendCommentAPI = ({ artId, content }) => {
  return request({
    url: '/v1_0/comments',
    method: 'post',
    data: {
      target: artId,
      content: content
    }
  })
}

// 获取用户基本资料
export const getUserInfoAPI = () => {
  return request({
    url: '/v1_0/user'
  })
}

// 获取个人资料
export const getUserProfileAPI = () => {
  return request({
    url: '/v1_0/user/profile'
  })
}

// 更新用户头像
export const updatePhotoAPI = (formObj) => {
  return request({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: formObj
  })
}

// 用户更新资料
export const updateUserInfoAPI = ({ userName, birthday }) => {
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: {
      name: userName,
      birthday
    }
  })
}
